package com.shuoyue.adven;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import java.io.File;


public class NOpenFiles {

    public static void openFile(Activity context, File currentPath) {
        if (currentPath != null && currentPath.isFile()) {
            if (currentPath.length() == 0) {
                Toast.makeText(context, "文件是空的", Toast.LENGTH_SHORT).show();
                return;
            }
            String fileName = currentPath.toString();
            Intent intent;
            try {
                if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingImage))) {
                    intent = getImageFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingWebText))) {

                    intent = getHtmlFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingPackage))) {
                    intent = getApkFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));

                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingAudio))) {
                    intent = getAudioFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingVideo))) {
                    intent = getVideoFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingText))) {
                    intent = getTextFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingPdf))) {
                    intent = getPdfFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingWord))) {
                    intent = getWordFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingExcel))) {
                    intent = getExcelFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else if (checkEndsWithInStringArray(fileName, context.getResources()
                        .getStringArray(R.array.fileEndingPPT))) {
                    intent = getPPTFileIntent(context, currentPath);
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                } else {
                    // pg.dismiss();
//                    Toast.makeText(context, "无法打开，请安装相应的软件！",
//                            Toast.LENGTH_SHORT).show();
                    intent = new Intent("android.intent.action.VIEW");
                    intent.addCategory("android.intent.category.DEFAULT");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        String atction = context.getPackageName() + ".provider";
                        Uri contentUri = FileProvider.getUriForFile(context, atction, currentPath);
                        intent.setDataAndType(contentUri, "*/*");
                    } else {
                        intent.setDataAndType(Uri.fromFile(currentPath), "*/*");
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    context.startActivity(Intent.createChooser(intent, "打开文件:" + currentPath.getName()));
                }
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "无法打开，请安装相应的软件！",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            // pg.dismiss();
            Toast.makeText(context, "对不起，不能识别该文件！",
                    Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * android获取一个用于打开HTML文件的intent
     */

    public static Intent getHtmlFileIntent(Activity context, File file) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "text/html");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "text/html");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开图片文件的intent
    public static Intent getImageFileIntent(Activity context, File file) {


        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "image/*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "image/*");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开PDF文件的intent
    public static Intent getPdfFileIntent(Activity context, File file) {


        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "application/pdf");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        }
        return intent;
    }

    // android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent(Activity context, File file) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "text/plain");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开音频文件的intent
    public static Intent getAudioFileIntent(Activity context, File file) {

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "audio/*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "audio/*");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开视频文件的intent
    public static Intent getVideoFileIntent(Activity context, File file) {


        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "video/*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "video/*");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开CHM文件的intent
    public static Intent getChmFileIntent(Activity context, File file) {


        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "application/x-chm");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/x-chm");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;

    }

    // android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent(Activity context, File file) {

        String type = "";
        if (file.getAbsolutePath().endsWith(".doc")) {
            type = "application/msword";
        } else {
            type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        }
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            uri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(uri, "application/msword");
        } else {
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, type);
            intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        }
        return intent;
    }

    // android获取一个用于打开Excel文件的intent
    public static Intent getExcelFileIntent(Activity context, File file) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "application/vnd.ms-excel");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    // android获取一个用于打开PPT文件的intent
    public static Intent getPPTFileIntent(Activity context, File file) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "application/vnd.ms-powerpoint");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-powerpoint");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        return intent;
    }

    // android获取一个用于打开apk文件的intent
    public static Intent getApkFileIntent(Activity context, File file) {
//        Intent intent = new Intent();
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction(Intent.ACTION_VIEW);
//        intent.setDataAndType(Uri.fromFile(file),
//                "application/vnd.android.package-archive");

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri uri = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            uri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
        } else {
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    public static void getIntentWithUrl(Activity context, File file) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String atction = context.getPackageName() + ".provider";
            Uri contentUri = FileProvider.getUriForFile(context, atction, file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

    }

    private static boolean checkEndsWithInStringArray(String checkItsEnd,
                                                      String[] fileEndings) {
        for (String aEnd : fileEndings) {
            if (checkItsEnd.toLowerCase().endsWith(aEnd))
                return true;
        }
        return false;
    }


}
